package com.littleken.sellproject.service;

import com.littleken.sellproject.dto.CartDTO;
import com.littleken.sellproject.enums.ResultEnum;
import com.littleken.sellproject.exception.SellException;
import com.littleken.sellproject.utils.JsonData;
import com.littleken.sellproject.dao.ProductInfoMapper;
import com.littleken.sellproject.enums.ProductStatusEnum;
import com.littleken.sellproject.model.ProductInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * 商品service
 */
@Service
@Slf4j
public class ProductInfoService {

    @Resource
    private ProductInfoMapper mapper;

    public ProductInfo findOne(String infoId){
        ProductInfo productInfo = null;
        try{
            productInfo = mapper.findById(infoId).get();
        }catch (NoSuchElementException e){
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        return productInfo;
    }

    /**
     *查询上架商品
     * @return
     */
    public List<ProductInfo> findUpAll(){
        return mapper.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    /**
     * 查询所有商品
     * @param pageable
     * @return
     */
    public Page<ProductInfo> findAll(Pageable pageable){
        return mapper.findAll(pageable);
    }


    public ProductInfo save(ProductInfo productInfo) {
        //return mapper.save(productInfo).addImageHost(upYunConfig.getImageHost());
        //todo:校验操作
        return mapper.save(productInfo);
    }

    public ProductInfo update(ProductInfo productInfo){
        return mapper.save(productInfo);
    }

    public ProductInfo update(String updateId,ProductInfo productInfo){
        if(!updateId.equals(productInfo.getProductId())){
            throw new SellException(ResultEnum.ERROR_UPDATE_BYID);
        }
        return mapper.save(productInfo);
    }


    /**
     * 加库存
     * @return
     */
    @Transactional
    public void addStock(List<CartDTO> cartDTOList){
        for(CartDTO cartDTO : cartDTOList){
            ProductInfo productInfo = mapper.findById(cartDTO.getProductId()).get();
            if(productInfo == null ){
                //抛商品不存在异常
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            //todo:需要加锁优化并发
            Integer result = productInfo.getProductStock() + cartDTO.getProductQuantity();

            productInfo.setProductStock(result);
            mapper.save(productInfo);
        }
    }

    /**
     * 扣库存
     * @return
     */
    @Transactional
    public void subtractStock(List<CartDTO> cartDTOList){
        for(CartDTO cartDTO : cartDTOList){
            ProductInfo productInfo = mapper.findById(cartDTO.getProductId()).get();
            if(productInfo == null ){
                //抛商品不存在异常
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            //todo:需要加锁优化并发
            Integer result = productInfo.getProductStock() - cartDTO.getProductQuantity();
            if(result < 0){
                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
            }
            productInfo.setProductStock(result);
            mapper.save(productInfo);
        }
    }

    /**
     * 产品上架
     * @param productId
     * @return
     */
    public ProductInfo onSale(String productId) {
        ProductInfo productInfo;
        try {
            productInfo = mapper.findById(productId).get();
        }catch (NoSuchElementException e){
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }

        if (productInfo.getProductStatusEnum() == ProductStatusEnum.UP) {
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }

        //更新
        productInfo.setProductStatus(ProductStatusEnum.UP.getCode());
        return mapper.save(productInfo);
    }

    /**
     * 产品下架
     * @param productId
     * @return
     */
    public ProductInfo offSale(String productId) {
        ProductInfo productInfo;
        try{
            productInfo = mapper.findById(productId).get();
        }catch (NoSuchElementException e){
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }

        if (productInfo.getProductStatusEnum() == ProductStatusEnum.DOWN) {
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }

        //更新
        productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());
        return mapper.save(productInfo);
    }

}
